package com.anrongtec.zcpt.openapi.util;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.provider.Settings.Secure;
import android.telephony.TelephonyManager;
import android.text.TextUtils;

import java.io.UnsupportedEncodingException;
import java.util.UUID;


/**
 * 
 * @author dongtianhao
 *
 */
@SuppressLint("NewApi")
public class DeviceFactory {
	private static final String PREFERENCENAME="device_info";
	protected static final String PREFS_DEVICE_UUID = "prefs_device_uuid";
	protected static final String PREFS_DEVICE_ID = "prefs_device_id";
	/** 手机制式类型 */
	protected static final String PREFS_PHONE_TYPE = "prefs_phone_type";
	protected static final String PREFS_MAC_ADDRESS = "prefs_mac_address";
	protected static final String PREFS_ANDROID_ID = "prefs_android_id";
	protected static final String PREFS_IMSI_CODE = "prefs_imsi_code";
	protected static UUID uuid;
	private static Context context;
	private static volatile DeviceFactory factory;
	private static SharedPreferences prefs;
	private TelephonyManager mTelephonyMgr;

	private DeviceFactory(Context ctx) {
		if (context == null) {
			context = ctx.getApplicationContext();
		}
	}

	public static DeviceFactory getInstance(Context context) {
		if (factory == null) {
			synchronized (DeviceFactory.class) {
				if (factory == null) {
					factory = new DeviceFactory(context);
				}
			}
		}
		return factory;
	}

	/**
	 *
	 * @Title: getDeviceUUID @Description:加密算法过后的设备ID，与设备无关，只做唯一约束性
	 *         设备管理应该直接用设备ID @param @return 设定文件 @return String 返回类型 @throws
	 */
	public String getDeviceUUID() {
		if (prefs == null) {
			prefs = context.getApplicationContext().getSharedPreferences(PREFERENCENAME, 0);
		}
		final String id = prefs.getString(PREFS_DEVICE_UUID, null);
		if (!TextUtils.isEmpty(id)) {
			return id;
			// uuid = UUID.fromString(id);
		} else {
			final String androidId = Secure.getString(context.getContentResolver(), Secure.ANDROID_ID);
			try {
				if (!"9774d56d682e549c".equals(androidId)) {
					uuid = UUID.nameUUIDFromBytes(androidId.getBytes("utf8"));
				} else {
					if (mTelephonyMgr == null) {
						mTelephonyMgr = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
					}
					final String deviceId = mTelephonyMgr.getDeviceId();
					// 权限获取失败，返回15个0，则不适合用于当做唯一标识了
					uuid = (null != deviceId && deviceId.startsWith("000000"))
							? UUID.nameUUIDFromBytes(deviceId.getBytes("utf8")) : UUID.randomUUID();

				}
			} catch (UnsupportedEncodingException e) {
				throw new RuntimeException(e);
			}

			// Write the value out to the prefs file
			String uid = uuid.toString().replace("-", "");
			prefs.edit().putString(PREFS_DEVICE_UUID, uid).commit();
			return uid;
		}
	}

	/**
	 *
	 * @Title: getIMEI @Description:
	 *         获取设备的IMEI码，如果是GSM网络，返回IMEI；如果是CDMA网络，返回MEID @return String
	 *         返回类型 @throws
	 */
	public String getIMEI() {
		if (prefs == null) {
			prefs = context.getApplicationContext().getSharedPreferences(PREFERENCENAME, 0);
		}
		String id = prefs.getString(PREFS_DEVICE_ID, null);
		if (!TextUtils.isEmpty(id)) {
			return id;
		} else {
			if (mTelephonyMgr == null) {
				mTelephonyMgr = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
			}
			id = mTelephonyMgr.getDeviceId();
			prefs.edit().putString(PREFS_DEVICE_ID, id).commit();
			return id;
		}
	}

	/**
	 *
	 * @Title: getIMSI @Description:获取ISMI码，SIM卡标识 @param @return @return String
	 *         返回类型 @throws
	 */
	public String getIMSI() {
		if (prefs == null) {
			prefs = context.getApplicationContext().getSharedPreferences(PREFERENCENAME, 0);
		}
		String imsi = prefs.getString(PREFS_IMSI_CODE, null);
		if (!TextUtils.isEmpty(imsi)) {
			return imsi;
		} else {
			if (mTelephonyMgr == null) {
				mTelephonyMgr = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
			}
			imsi = mTelephonyMgr.getSubscriberId();
			prefs.edit().putString(PREFS_IMSI_CODE, imsi).commit();
			return imsi;
		}
	}

	/**
	 * 获取手机类型,厂商类型
	 *
	 * @Title: getPhoneModel @Description: TODO(这里用一句话描述这个方法的作用) @return String
	 *         返回类型 @throws
	 */
	public String getPhoneModel() {

		return Build.MODEL;

	}
	/**
	 * 获取手机制造商
	 *
	 * @Title: getPhoneModel @Description: TODO(这里用一句话描述这个方法的作用) @return String
	 *         返回类型 @throws
	 */
	public String getManufacturerl() {

		return Build.MANUFACTURER;

	}

	/**
	 * 获取手机制式类型
	 *
	 * @Title: getPhoneType @Description: TODO(这里用一句话描述这个方法的作用) @return int
	 *         返回类型 @returnValue int PHONE_TYPE_CDMA 手机制式为CDMA，电信 @returnValue
	 *         int PHONE_TYPE_GSM 手机制式为GSM，移动和联通 @returnValue int
	 *         PHONE_TYPE_NONE 手机制式未知 @throws
	 */
	public int getPhoneType() {
		if (prefs == null) {
			prefs = context.getApplicationContext().getSharedPreferences(PREFERENCENAME, 0);
		}
		int type = prefs.getInt(PREFS_PHONE_TYPE, -1);
		if (type != -1) {
			return type;
		} else {
			if (mTelephonyMgr == null) {
				mTelephonyMgr = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
			}
			type = mTelephonyMgr.getPhoneType();
			prefs.edit().putInt(PREFS_PHONE_TYPE, type).commit();
			return type;
		}
	}

	/**
	 * 获取mac地址
	 *
	 * @Title: getMacAddress @Description: TODO(这里用一句话描述这个方法的作用) @param @return
	 *         设定文件 @return String 返回类型 @throws
	 */
	public String getMacAddress() {
		if (prefs == null) {
			prefs = context.getApplicationContext().getSharedPreferences(PREFERENCENAME, 0);
		}
		String mac = prefs.getString(PREFS_MAC_ADDRESS, null);
		if (!TextUtils.isEmpty(mac)) {
			return mac;
		} else {
			WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
			WifiInfo wifiInfo = wifiManager.getConnectionInfo();
			mac = wifiInfo.getMacAddress();
			prefs.edit().putString(PREFS_MAC_ADDRESS, mac);
			return mac;
		}

	}

	/**
	 * 获取设备序列号
	 *
	 * @return
	 */
	public String getDeviceSN() {
		if (prefs == null) {
			prefs = context.getApplicationContext().getSharedPreferences(PREFERENCENAME, 0);
		}
		String androidId = prefs.getString(PREFS_ANDROID_ID, null);
		if (!TextUtils.isEmpty(androidId)) {
		} else {
			androidId = Secure.getString(context.getContentResolver(), Secure.ANDROID_ID);
			prefs.edit().putString(PREFS_ANDROID_ID, androidId);
		}
		return androidId;
	}

	/**
	 * Role:Telecom service providers获取手机服务商信息 <BR>
	 * 需要加入权限<uses-permission
	 * android:name="android.permission.READ_PHONE_STATE"/> <BR>
	 * Date:2012-3-12 <BR>
	 *
	 * @author CODYY)peijiangping
	 */
	public String getProvidersName() {
		String ProvidersName = null;
		// 返回唯一的用户ID;就是这张卡的编号神马的
		String IMSI = getIMSI();
		// IMSI号前面3位460是国家，紧接着后面2位00 02是中国移动，01是中国联通，03是中国电信。
		System.out.println(IMSI);
		if (IMSI.startsWith("46000") || IMSI.startsWith("46002")) {
			ProvidersName = "中国移动";
		} else if (IMSI.startsWith("46001")) {
			ProvidersName = "中国联通";
		} else if (IMSI.startsWith("46003")) {
			ProvidersName = "中国电信";
		}
		return ProvidersName;
	}

}